<?php

namespace app\admin\controller;

use think\exception\ValidateException;
use app\model\Order;
use app\model\OrderStaff;
use app\model\Store;
use app\model\OrderStatus;
use app\model\Paymethod;
use app\model\Member;
use app\model\MemberCashlogs;
use app\model\Category;
use app\model\RefundAddress;
use app\model\OrderRefund;
use app\model\Message;
use app\model\Operatingcity;

class OrderrefundController extends Base
{

	function index()
	{
		$weid = weid();
		$keyword = input('post.keyword', '', 'serach_in');
		$query = Order::where(['weid' => $weid, 'order_status_id' => 6]);

		if (!empty($this->sid)) {
			$query->where('sid', $this->sid);
		}
		if (!empty($this->ocid)) {

			$Operatingcitydata = Operatingcity::find($this->ocid);
			if ($Operatingcitydata) {
				$Operatingcitydata = $Operatingcitydata->toArray();

				if ($Operatingcitydata['areatype'] == 2) {
					$query->where('shipping_district_name', $Operatingcitydata['district_name']);
				} elseif ($Operatingcitydata['areatype'] == 1) {
					$query->where('shipping_city_name', $Operatingcitydata['city_name']);
				}
			}
		}

		$query->with(['orderRefund', 'member', 'orderGoods', 'paymethod']);

		if (!empty($keyword)) {
			$query->where('name', 'like', '%' . $keyword . '%');
		}

		$res = $query->order('id desc')
			->paginate(getpage())
			->toArray();

		foreach ($res['data'] as &$vo) {

			$vo['sid'] = Store::getTitle($vo['sid']);
			$vo['staff'] = OrderStaff::staff($vo['id']);
			$vo['create_time'] = time_format($vo['create_time']);
			$vo['pay_time'] = time_format($vo['pay_time']);

			if (empty($vo['orderGoods'])) {
				if (!empty($vo['cat_id'])) {
					$vo['orderGoods'][0]['image'] = Category::getImage($vo['cat_id']);
				}
				$vo['orderGoods'][0]['name'] = '【用户发布需求】' . $vo['pay_subject'];
			}
			if ($vo['ptype'] == 1) {
				$vo['order_status'] = OrderStatus::get_order_status_name($vo['order_status_id']);
				$vo['order_status'] = $vo['order_status'] . '【' . refundStatus(($vo['orderRefund']['refund_status'])) . '】';
				$vo['orderRefund']['refund_type_name'] = refundType($vo['orderRefund']['refund_type']);
			} else {
				$vo['order_status'] = OrderStatus::get_order_status_name_yuyue($vo['order_status_id']);
				$vo['order_status'] = $vo['order_status'] . '【' . refundStatus_yuyue(($vo['orderRefund']['refund_status'])) . '】';
				$vo['orderRefund']['refund_type_name'] = refundType_yuyue($vo['orderRefund']['refund_type']);
			}


			$vo['payment_code_name'] = 	Paymethod::get_paymethod_name($vo['payment_code']);

			$vo['shipping_type'] = getdeliverymode($vo['deliverymode']);

			if ($vo['is_times'] == 1) {

				if ($vo['timesdate']) {
					if ($vo['timestype'] == 1) {
						$vo['yue_time'] = '每月:' . $vo['timesdate'] . '号';
					} else {
						$vo['yue_time'] = '每周周:' . $vo['timesdate'];
					}
				} else {
					$vo['yue_time'] = '还没有预约时间';
				}
			} else {
				$vo['yue_time'] = time_format($vo['beginTime']) . ' 到 ' . date('H:i', $vo['endTime']);
			}

			if (!empty($vo['orderGoods'])) {
				foreach ($vo['orderGoods'] as &$vvo) {
					$vvo['image'] = toimg($vvo['image']);
				}
			}
		}

		$data['data'] = $res;
		$RefundAddress = RefundAddress::where(['weid' => weid(), 'status' => 1])->order('id asc')->select()->toArray();
		$data['field_data']['RefundAddress'] = $RefundAddress;

		return $this->json($data);
	}

	function getInfo()
	{
		$id =  $this->request->post('id', '', 'serach_in');
		if (!$id) throw new ValidateException('参数错误');

		$data = Order::order_info($id);

		if ($data['orderInfo']['sid'] == 0) {
			$data['orderInfo']['sid'] = '';
		}

		return $this->json(['data' => $data]);
	}

	// 处理售后订单
	public function refundhandle()
	{
		$postdata = input('post.');
		// $res = Message::sendMiniRefundsuccess($postdata);
		// throw new ValidateException($res);
		try {
			$postdata['OrderRefund']['response_time'] = time();

			$r = OrderRefund::where('id', $postdata['OrderRefund']['id'])->update($postdata['OrderRefund']);
			if ($r && $postdata['OrderRefund']['refund_status'] == 1) {
				$r = Order::refund_order($postdata);
			}
		} catch (\Exception $e) {
			throw new ValidateException($e->getMessage());
		}

		if ($r) {
			return $this->json(['msg' => '处理成功！']);
		} else {
			throw new ValidateException('处理失败');
		}
	}

	function delete()
	{
		$idx =  $this->request->post('id', '', 'serach_in');
		if (!$idx) throw new ValidateException('参数错误');
		Order::destroy(['id' => explode(',', $idx)], true);
		OrderRefund::where(['order_id' => explode(',', $idx)])->delete();
		return $this->json(['msg' => '操作成功']);
	}
}
